home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / dos / secdr13d / login.c < prev    next >
C/C++ Source or Header  |  1994-04-20  |  15KB  |  554 lines

  1. /* Secure Drive LOGIN V1.3d */
  2. /* Logs into encrypted disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. extern char pass1[MAXPASS];
  7. extern char pass2[MAXPASS];
  8. extern char Old13Check[4];
  9.  
  10. extern int  tsr_not_installed;
  11. extern int  tsr_wrong_version;
  12. extern int  df10,ef10;
  13. extern int  incfo,outcfo;     /*1.3c*/
  14. extern char compat_mode;
  15.  
  16. int   setenv(char*,char *);
  17. void  clrbufs(void);
  18. void upd13cw (void);
  19. void rcfproc(void);
  20. void ucfoproc(void *);
  21.  
  22. char *pgpv;
  23. char drvltr;
  24. unsigned safemode=FALSE;
  25. unsigned pgpsw=FALSE;
  26. unsigned ucfosw=FALSE;
  27. unsigned rcfsw=FALSE;
  28. unsigned char buf[512],key[16],check[4];
  29. unsigned drive,firstcyl,firsthead,maxcyl;
  30. unsigned maxhead,maxsector,secsize,i;
  31.  
  32. int main(int argc,char *argv[])
  33. {
  34. word16 expkey[52];
  35. unsigned serial[2];
  36. unsigned char *p;
  37. int ekeyexp=0;
  38.  
  39. if(argc==1)
  40.  {
  41.     printf("\n\
  42. Secure Drive Login Version 1.3D\n\
  43. This program sets parameters and loads passphrases for encrypted\
  44.  drives.\n\n\
  45. LOGIN /F  [/PGP] [/UCFO] [/RCF]  to enter floppy disk passphrase\n\
  46.                                  [use/set PGPPASS] [Update CryptFlag\n\
  47.                                  Offset] [Recover CryptFlag]\n\
  48. \n\
  49. LOGIN /C  [/PGP]                 to erase keys and disable encryption\n\
  50.                                  [clear PGPPASS]\n\
  51. \n\
  52. LOGIN drive letter  [/PGP]       to activate an encrypted hard drive\n\
  53.   [/UCFO] [/RCF]                 \n\
  54. LOGIN D: /S                      to prevent accidental access to an\n\
  55.                                  encrypted drive without logging in\n\
  56. \n\
  57. LOGIN /PGP                       to set PGPPASS environment variable\n\
  58. \n\
  59. LOGIN drive cylinder head        to manually enter parameters for\n\
  60.  [/PGP] [/UCFO] [/RCF]           a hard drive partition\n\
  61. LOGIN 0 100 1   /S               to prevent accidental access\n\
  62.       drives are numbered from zero\n\n");
  63.     exit(1);
  64.   }
  65.  
  66.  
  67. for (i=1;i<argc;i++)
  68.  {
  69.    if (!stricmp(argv[i],"/PGP"))
  70.     {
  71.      pgpsw=TRUE;
  72.      pgpv=getenv("PGPPASS");
  73.     }
  74.    if (!stricmp(argv[i],"/UCFO"))
  75.     ucfosw=TRUE;
  76.    if (!stricmp(argv[i],"/RCF"))
  77.     rcfsw=TRUE;
  78.  }
  79.  
  80. if (argc == 2 && pgpsw)
  81.  {
  82.   printf("\nEnter PGPPASS passphrase for PGP Secret Key: ");
  83.   invgets(pass1);
  84.   if(pass1[0] != 0)
  85.    {
  86.     if (!setenv("PGPPASS",pass1))
  87.      {
  88.       printf("PGPPASS set. \n");
  89.       clrbufs();
  90.       exit(0);
  91.      }
  92.     else
  93.      {
  94.       printf("PGPPASS not set. \n");
  95.       clrbufs();
  96.       exit(1);
  97.      }
  98.    }
  99.   else
  100.    {
  101.     if (pgpv != NULL)
  102.      {
  103.       for (i=0;i<strlen(pgpv);i++)
  104.        pass1[i]='x';
  105.       pass1[i]=0;
  106.       setenv("PGPPASS",pass1);
  107.       setenv("PGPPASS","");
  108.       printf("PGPPASS removed from environment\n");
  109.       clrbufs();
  110.       exit(0);
  111.      }
  112.     else
  113.      {
  114.       printf("PGPPASS not removed from environment because not present\n");
  115.       clrbufs();
  116.       exit(1);
  117.      }
  118.    }
  119.  }
  120.  
  121. set_compat_mode();
  122.  
  123. if(!(cryptdata=gettsradr()))
  124.  {
  125.   printf("\nError: Secure Drive TSR %s.\n",
  126.          tsr_wrong_version ? "wrong Version" : "not loaded");
  127.   exit(1);
  128.  }
  129.  
  130.  
  131.  
  132. if((*argv[1]=='/')&&(toupper(*(argv[1]+1))=='C'))
  133. {
  134.   bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  135.   for (i=0;i<MAXDRV;i++)
  136.    cryptdata->hd[i].active=0;
  137.  
  138.   memset(cryptdata->fkey,0xaa,104);
  139.   memset(cryptdata->hkey,0xbb,104);
  140.   memset(cryptdata->fkeychk,0x0ff,4);
  141.   memset(cryptdata->hkeychk,0x0ff,4);
  142.   printf("\nClearing free memory...");
  143.   while ((p=malloc(1024)) != NULL)
  144.    {
  145.     memset(p,0,1024);
  146.    }
  147.   while ((p=malloc(8)) != NULL)
  148.    {
  149.     memset(p,0,8);
  150.    }
  151.  
  152.    printf("\nAll keys erased. System secured.\n");
  153.    if (pgpsw && pgpv != NULL)
  154.     {
  155.      for (i=0;i<strlen(pgpv);i++)
  156.       pass1[i]='x';
  157.      pass1[i]=0;
  158.      setenv("PGPPASS",pass1);
  159.      setenv("PGPPASS","");
  160.      printf("PGPPASS removed from environment\n");
  161.     }
  162.   clrbufs();
  163.   exit(0);
  164.  }
  165.  
  166.  
  167. if((*argv[1]=='/')&&(toupper(*(argv[1]+1))=='F'))
  168.  {
  169.   char c;
  170.   int fdpr=1;
  171.   firsthead=0;       /*1.3b*/
  172.   firstcyl=0;
  173.   while (1)
  174.    {
  175.     printf("\nInsert encrypted diskette in drive A or B\n\
  176. then press A or B to check passphrase.\n\
  177. Or Press Esc to %s.: ",
  178.        ucfosw || rcfsw ? "exit" : "bypass passphrase check");  /*1.3c*/
  179.  
  180.     while ((c=toupper(getch())) != 'A' && c != 'B' && c != ESC) {}
  181.     if (c == ESC)
  182.      {
  183.       if(!ucfosw && !rcfsw)
  184.        {
  185.         printf("\n\n");                 /*1.3c*/
  186.         break;                          /*1/3c*/
  187.        }
  188.       else
  189.        exit(1);
  190.      }
  191.     printf("%c\n\n",c);
  192.     drvltr=c;
  193.     drive=(unsigned) c-'A';
  194.     readsec(drive,0,0,1,1,buf);
  195.     if((buf[510]!=0x55)||(buf[511]!=0xaa))
  196.      {
  197.       printf("This diskette is not formatted.\n\n");
  198.       continue;
  199.      }
  200.     if(memcmp(buf+incfo,"CRYP",4) && memcmp(buf+outcfo,"CRYP",4) && !rcfsw) /*1.3c*/
  201.      {
  202.       printf ("This diskette is not encrypted.\n\n");
  203.       continue;
  204.      }
  205.     break;
  206.    } /* while (1)*/
  207.  
  208.   {
  209.    unsigned char temp[16];
  210.    unsigned char nokey [104];
  211.    MD5_CTX md5buf;
  212.  
  213.    memset(nokey,0xbb,104);
  214.    if (memcmp(cryptdata->hkey,nokey,104) != 0)
  215.     {
  216.      if (c != ESC)
  217.       {
  218.        if(!memcmp(cryptdata->hkeychk,buf+incfo+4,4) ||
  219.           !memcmp(cryptdata->hkeychk,buf+outcfo+4,4))
  220.         {
  221.          memcpy(check,cryptdata->hkeychk,4);
  222.          memcpy(expkey,cryptdata->hkey,104);
  223.          ef10 = df10 = cryptdata->fkeyv10 = cryptdata->hkeyv10;
  224.          ekeyexp=1;
  225.          fdpr=0;
  226.          printf("Floppy disk encryption enabled with\n\
  227. verified active hard disk partition %s passphrase.\n",
  228.           df10 ? "(V 1.0)" : "(V 1.1)");
  229.           ucfoproc(cryptdata->hkeychk);  /*Update CryptFlag Offset if /UCFO  1.3c*/
  230.  
  231.  
  232.         }
  233.       }
  234.      else /*verify bypassed*/
  235.       {
  236.        printf("Use active Hard Disk passphrase as unverified floppy passphrase? :");
  237.        if (getyn())
  238.         {
  239.          memcpy(check,cryptdata->hkeychk,4);
  240.          memcpy(expkey,cryptdata->hkey,104);
  241.          ef10 = cryptdata->hkeyv10;
  242.          ekeyexp=1;
  243.          fdpr=0;
  244.          printf("Floppy disk encryption enabled with\n\
  245. unverified active hard disk partition %s passphrase.\n",
  246.           ef10 ? "(V 1.0)" : "(V 1.1)");
  247.         }
  248.       }
  249.     }
  250.   }
  251.  
  252.    if (fdpr && pgpsw && pgpv != NULL)
  253.     {
  254.      strcpy(pass1,pgpv);
  255.      if (c != ESC)
  256.       {
  257.        setkeydf(key,check,buf);
  258.        upd13cw();         /*update 1.3 checkword to 1.3a */  /*1/3c*/
  259.  
  260.        ef10=df10;
  261.        if(!memcmp(check,buf+incfo+4,4) || !memcmp(check,buf+outcfo+4,4))
  262.         {
  263.          printf("\nPGPPASS entered as %s floppy disk passphrase\n",
  264.                df10 ? "(V 1.0)" : "(V 1.1)");
  265.          ucfoproc(check);     /*Update CryptFlag Offset if /UCFO  1.3c*/
  266.          fdpr=0;
  267.         }
  268.        else
  269.         {
  270.          printf("\nPGPPASS is wrong passphrase.\n");
  271.          fdpr=1;
  272.         }
  273.       }
  274.      else
  275.       {
  276.        setkeye (key,check);
  277.        printf("\nPGPPASS entered as %s unverified floppy passphrase\n",
  278.                ef10 ? "(V 1.0)":"(V 1.1)");
  279.        fdpr=0;
  280.       }
  281.     }
  282.    if (fdpr)
  283.     {
  284.      printf("\nEnter floppy disk passphrase: ");
  285.      if (c != ESC)
  286.       {
  287.        getkeydf(key,check,buf);
  288.        upd13cw();         /*update 1.3 checkword to 1.3a */  /*1/3c*/
  289.        ef10=df10;
  290.        if(!memcmp(check,buf+incfo+4,4) || !memcmp(check,buf+outcfo+4,4))
  291.         {
  292.          printf("Floppy disk encryption enabled with\n");
  293.          printf("verified %s passphrase.\n",
  294.                  df10 ? "(V 1.0)" : "(V 1.1)");
  295.          ucfoproc(check);  /*Update CryptFlag Offset if /UCFO  1.3c*/
  296.         }
  297.        else
  298.         rcfproc();  /*Incorrect Passphrase or /RCF*/  /*1.3c*/
  299.       }
  300.      else
  301.       {
  302.        getkey(key,check,FALSE);
  303.        printf("Floppy disk encryption enabled with\n");
  304.        printf("Unverified %s passphrase.\n",
  305.                ef10 ? "(V 1.0)":"(V 1.1)");
  306.       }
  307.     }
  308.    if (!ekeyexp)
  309.     en_key_idea((word16 *) key,expkey);
  310.    memcpy(cryptdata->fkey,expkey,104);
  311.    memcpy(cryptdata->fkeychk,check,4);
  312.    cryptdata->fkeyv10=(char) ef10;
  313.    cryptdata->fda.firstcyl=0;
  314.    cryptdata->fdb.firstcyl=0;
  315.    if (pgpsw && pgpv == NULL && !setenv("PGPPASS",pass1))
  316.     printf("PGPPASS set. \n");                /*1.3b*/
  317.    bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  318.    clrbufs();
  319.    exit(0);
  320.  }
  321.  
  322. else if(isalpha(*argv[1]))
  323.  {
  324.   drvltr=toupper(*argv[1]);
  325.   drive=255;
  326.   readptbl(drvltr,&drive,&firsthead,&firstcyl);   /*1.1*/
  327.   if(drive==255) {
  328.       printf("\nDrive not found.\n");
  329.       exit(1); }
  330.   if((*argv[2]=='/')&&(toupper(*(argv[2]+1))=='S'))
  331.       safemode=TRUE;
  332.   printf("\nDrive %c is physical hard drive %u, cylinder %u, head %u\n",
  333.           drvltr,drive,firstcyl,firsthead);     /*1.3b*/
  334.  }
  335.  
  336. else
  337.  {
  338.   drvltr='X';
  339.   i=sscanf(argv[1],"%u",&drive);
  340.   i=i&&sscanf(argv[2],"%u",&firstcyl);
  341.   i=i&&sscanf(argv[3],"%u",&firsthead);
  342.   if((*argv[4]=='/')&&(toupper(*(argv[4]+1))=='S'))
  343.       safemode=TRUE;
  344.   if(!i)
  345.    {
  346.     printf("Incorrect drive, cylinder, or head input.\n\
  347. Run without a command line for help.\n");
  348.     exit(1);
  349.    }
  350.  }
  351.  
  352. drive+=0x80;
  353.  
  354. readsec(drive,firsthead,firstcyl,1,1,buf);
  355.  
  356. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  357.     printf("This is not a boot sector.\n");
  358.     exit(1); }
  359.  
  360. calcdiskparams(buf,&maxcyl,&maxhead,&maxsector,
  361.                &secsize,serial);
  362.  
  363. printf("Disk %c: has: %i cylinders, %i sectors, %i heads, sector \
  364. size %i bytes\n",drvltr,maxcyl+1,maxsector,maxhead,secsize);
  365.  
  366. if(memcmp(buf+incfo,"CRYP",4) && memcmp(buf+outcfo,"CRYP",4) && !rcfsw) /*1.3c*/
  367.  {
  368.   printf("This disk is not encrypted.\n\n");
  369.   exit(1);
  370.  }
  371.  
  372.  
  373. if(!safemode)
  374. {
  375.  int hdpr=1;
  376.  if (pgpsw && pgpv != NULL)
  377.   {
  378.    strcpy(pass1,pgpv);
  379.    setkeydf(key,check,buf);
  380.    upd13cw();         /*update 1.3 checkword to 1.3a */  /*1/3c*/
  381.    if(memcmp(check,buf+incfo+4,4) && memcmp(check,buf+outcfo+4,4))
  382.     {
  383.      printf("\nPGPPASS is wrong passphrase.\n");
  384.      hdpr=1;
  385.     }
  386.    else
  387.     {
  388.      printf("\nPGPPASS entered as %s hard disk passphrase.\n",
  389.             df10 ? "(V 1.0)" : "(V 1.1)");
  390.      ucfoproc(check);  /*Update CryptFlag Offset if /UCFO  1.3c*/
  391.      hdpr=0;
  392.     }
  393.   }
  394.  if (hdpr)
  395.   {
  396.    printf("\nEnter hard disk passphrase: ");
  397.    getkeydf(key,check,buf);
  398.    upd13cw();         /*update 1.3 checkword to 1.3a */  /*1/3c*/
  399.   }
  400.  
  401.  if(memcmp(check,buf+incfo+4,4) && memcmp(check,buf+outcfo+4,4))
  402.   rcfproc();  /*Incorrect Passphrase or /RCF*/  /*1.3c*/
  403.  else
  404.   ucfoproc(check);  /*Update CryptFlag Offset if /UCFO  1.3c*/
  405.  
  406.  
  407.  en_key_idea((word16 *)key,expkey);
  408.  
  409.  memcpy(cryptdata->hkey,expkey,104);
  410.  memcpy(cryptdata->hkeychk,check,4);
  411.  cryptdata->hkeyv10=(char) df10;
  412. }
  413.  
  414. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  415. /*set all slots inactive*/
  416. for (i=0;i<MAXDRV;i++)
  417.  {
  418.    if (cryptdata->hd[i].active)
  419.     printf("Drive %c: set to Safe mode.\n",cryptdata->hd[i].drvltr);
  420.    cryptdata->hd[i].active=0;
  421.  }
  422.  
  423. /*find empty or duplicate HD slot*/
  424. for (i=0;i<MAXDRV;i++)
  425.  {
  426.    if (  cryptdata->hd[i].dddrv    == 0  ||
  427.         (cryptdata->hd[i].dddrv    == drive &&
  428.          cryptdata->hd[i].firstcyl == firstcyl) )
  429.     {
  430.      cryptdata->hd[i].dddrv=drive;
  431.      cryptdata->hd[i].drvltr=drvltr;
  432.      cryptdata->hd[i].firstcyl=firstcyl;
  433.      cryptdata->hd[i].firsthd=firsthead;
  434.      cryptdata->hd[i].firstsec=1;
  435.      cryptdata->hd[i].lastcyl=firstcyl+maxcyl;
  436.      cryptdata->hd[i].maxsec=maxsector;
  437.      cryptdata->hd[i].maxhd=maxhead;
  438.      cryptdata->hd[i].secsize=secsize;
  439.      if(!safemode)
  440.       {
  441.        cryptdata->hd[i].serial[0]=serial[0];
  442.        cryptdata->hd[i].serial[1]=serial[1];
  443.        cryptdata->hd[i].active=1;
  444.        printf("\nHard disk %s encryption enabled logical drive %c:.\n",
  445.               df10 ? "(V 1.0)" : "(V 1.1)",
  446.               drvltr);
  447.        if (pgpsw && pgpv == NULL && !setenv("PGPPASS",pass1))
  448.         printf("PGPPASS set. \n");                /*1.3c*/
  449.        break;
  450.       }
  451.      else
  452.       {
  453.        printf("\nHard disk set to safe mode.\n");
  454.        break;
  455.       }
  456.     }
  457.  }
  458.  
  459. if (i == MAXDRV)
  460.  printf("\nERROR: More than %d Encrypted Partitions not supported\n",MAXDRV);
  461.  
  462. for(i=0;i<16;i++) key[i]='\0';
  463. for(i=0;i<52;i++) expkey[i]=0;
  464.  
  465. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  466. clrbufs();
  467. return(0);
  468. }
  469.  
  470. void upd13cw (void)
  471.  {
  472.       if(!df10 && !memcmp(Old13Check,buf+outcfo+4,4))
  473.        {
  474.         printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  475.                drvltr);
  476.         if(getyn())
  477.          {
  478.           if (memcmp(buf+incfo,"CRYP",4))
  479.            memcpy(buf+incfo,"MSDOS   ",8);
  480.           memcpy(buf+outcfo,"CRYP",4);
  481.           memcpy(buf+outcfo+4,check,4);
  482.           writesec(drive,firsthead,firstcyl,1,1,buf);
  483.          }
  484.        }
  485.  
  486.       else if(!df10 && !memcmp(Old13Check,buf+incfo+4,4) && ucfosw)  /*1.3c*/
  487.        {
  488.         printf("\
  489. Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A.\n\
  490. CryptFlag Offset will also be changed to %d. Proceed? ",
  491.                drvltr,outcfo);
  492.         if(getyn())
  493.          {
  494.           memcpy(buf+outcfo,"CRYP",4);
  495.           memcpy(buf+outcfo+4,check,4);
  496.           memcpy(buf+incfo,"MSDOS   ",8);
  497.           writesec(drive,firsthead,firstcyl,1,1,buf);
  498.          }
  499.        }
  500.  }
  501.  
  502. void rcfproc(void)
  503.  {
  504.   if (!rcfsw)
  505.    {
  506.     printf("Wrong passphrase.\n");
  507.     exit(1);
  508.    }
  509.   else
  510.    {
  511.     printf("\nPassphrase Entered does not match CryptFlag.\n");
  512.     if (compat_mode == 'X')
  513.      {
  514.       printf("You specified /RCF, but this is not allowed with SD10CMP=X\n");
  515.       exit(1);
  516.      }
  517.     else
  518.      {
  519.       printf("\n\
  520. /RCF Specified. Re-Enter Same Passphrase to Ignore present CryptFlag,\n\
  521. compute new %s CryptFlag and write it at offset %d.\n\n\
  522. Re-Enter passphrase: ",
  523.       compat_mode != 'N' ? "(V 1.0)":"(V 1.1)", outcfo);
  524.       strcpy(pass2,pass1);
  525.       getkey(key,check,FALSE);
  526.       if (!strcmp(pass1,pass2))
  527.        {
  528.         if (!memcmp(buf+incfo,"CRYP",4))
  529.          memcpy(buf+incfo,"MSDOS   ",8);
  530.         memcpy(buf+outcfo,"CRYP",4);
  531.         memcpy(buf+outcfo+4,check,4);
  532.         writesec(drive,firsthead,firstcyl,1,1,buf);
  533.         printf("\nCryptFlag Reconstructed.\n");
  534.        }
  535.       else
  536.        {
  537.         printf("Passphrases do not match.\n");
  538.         exit(1);
  539.        }
  540.      }
  541.    }
  542.  } /*rcfproc*/
  543.  
  544. void ucfoproc(void *keychk)
  545.  {
  546.   if (!ucfosw) return;
  547.   if (memcmp(buf+incfo,"CRYP",4)) return;
  548.   memcpy(buf+incfo,"MSDOS   ",8);
  549.   memcpy(buf+outcfo,"CRYP",4);
  550.   memcpy(buf+outcfo+4,keychk,4);
  551.   writesec(drive,firsthead,firstcyl,1,1,buf);
  552.   printf("CryptFlag written at offset %d\n",outcfo);
  553.  } /*ucfoproc*/
  554.